﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameView.Comment;
using GameView.Models;
using GameView.Services;
using System.Collections.ObjectModel;

namespace GameView.ViewModels
{
    public partial class StatisticsByMeViewModel : ObservableObject
    {
        private RechargeToMService _service;

        public StatisticsByMeViewModel(RechargeToMService service)
        {
            _service = service;
            LoadDataAsync().ConfigureAwait(false);
        }

        [ObservableProperty]
        private ObservableCollection<StatisticsEntity> _listOne = [];

        [ObservableProperty]
        private bool _isFeatureEnabled = false;

        [ObservableProperty]
        private RechargeQueryDto _queryDto = new();

        [ObservableProperty]
        private bool _isLoading = false;

        [ObservableProperty]
        private long _recharge = 0;

        [ObservableProperty]
        private long _withdrawal = 0;

        [ObservableProperty]
        private string _mainName = "商行统计";

        private async Task LoadDataAsync()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                QueryDto.ProjectName = string.Empty;
                var p = await _service.GetByMerchantAsync(QueryDto);
                Recharge = p.Data.Sum(q => q.Recharge);
                Withdrawal = p.Data.Sum(q => q.Withdrawal);

                ListOne = [.. p.Data];
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowError($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }

        [RelayCommand]
        private async Task SearchAsync()
        {
            await LoadDataAsync();
        }
    }
}